package com.innovation.wjrpc.convert;

import com.innovation.wjrpc.codec.Decoder;
import com.innovation.wjrpc.codec.impl.JSONDecoder;
import com.innovation.wjrpc.protocol.Response;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;

import java.util.List;

/**
 * @author innoyiya
 * @date 2021/9/8 10:52
 */
public class ByteToJava extends ByteToMessageDecoder {

    private Decoder decoder = new JSONDecoder();

    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
        byte[] bytes = new byte[in.readableBytes()];
        in.readBytes(bytes);
        out.add(decoder.decode(bytes, Response.class));
    }
}